In [39]:
# Import all libraries needed for the tutorial

# General syntax to import specific functions in a library: 
##from (library) import (specific library function)
from pandas import DataFrame, read_csv

# General syntax to import a library but no functions: 
##import (library) as (give the library a nickname/alias)
import matplotlib.pyplot as plt
import pandas as pd #this is how I usually import pandas
import sys #only needed to determine Python version number
import matplotlib.pyplot as plt
import seaborn 
from nltk.corpus import stopwords
import folium as folium
from wordcloud import WordCloud
# Enable inline plotting
%matplotlib inline
In [40]:
Input=pd.read_csv(r'/home/prokopis/Desktop/Data Mining-Εργασία 1/train.csv')
###Opening the train.csv to take info
In [41]:
#1.1
e=Input['room_type'].value_counts().nlargest(1)
print('Common type room_type')
print(e)
e.plot(kind='bar')
#Στο ερώτημα αυτό βρίσκω το πρώτο στοιχείο σε αιρθμό εμφανίσεων στα δεδομένα μου 
Common type room_type
Entire home/apt    24765
Name: room_type, dtype: int64
Out[41]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8d067e7810>
In [42]:
#1.2
print('Price graph')
fig, ax = plt.subplots(figsize=(8,6))
Input.groupby('Month').plot(kind='line', x = "Month", y = "price", ax=ax)
plt.show()
###Για κάθε μήνα βρίσκω τις τιμές που κυμαίνονται 
## 1-Φεβρουάριος
## 2- Μάρτιος
## 3-Απρίλιος
## ΔΕ ξέρω κατά πόσο έφτιαξα αυτό που θέλατε ,απλώς αυτό κατάλαβα στις συζητήσεις στο eclass
Price graph
In [43]:
#1.4
print('Top real Estate Neighbourhood')
e=Input['neighbourhood'].value_counts().nlargest(1)
print(e)
e.plot(kind='bar')
### Στο ερώτημα αυτό βρίσκω μέσω της value_counts την περιοχή η οποία εμφανίζεται στα περισσότερα entries 
Top real Estate Neighbourhood
ΕΜΠΟΡΙΚΟ ΤΡΙΓΩΝΟ-ΠΛΑΚΑ    4192
Name: neighbourhood, dtype: int64
Out[43]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8d0680e510>
In [44]:
#1.6
e=Input['neighbourhood'].value_counts()
e.plot(kind='bar',figsize=(20,10))
### Εκτύπωση ιστογράμματος μεταβλητής neighbourhood
Out[44]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8d069e3350>
In [45]:
#1.5-Month
Input.groupby('Month')['id'].nunique().plot(kind='bar')
print(Input.groupby('Month')['id'].nunique())
plt.show()
## Ομαδοποιώ το input προκειμένου να βρω μέσω του column id πόσες καταχωρήσεις έχω για κάθε μήνα 
Month
1    9100
2    9361
3    9661
Name: id, dtype: int64
In [46]:
#1.5-Neighbourhood
e=Input.groupby(['neighbourhood']).count()
g=e['id']
g.plot(kind='bar',figsize=(20,10))
plt.show()
## Ομαδοποιώ το input προκειμένου να βρω μέσω του column id πόσες καταχωρήσεις έχω για κάθε γειτονιά 
In [47]:
#1.7
e=Input.groupby(['neighbourhood','room_type'])
t=Input['neighbourhood'].unique()
h=len(t)
g=e.count().nlargest(h,'id')
loi=g['id']
loi.plot(kind='bar',figsize=(20,10))
plt.ylabel('Number of entries')
plt.show()
### Αρχικά βρίσκω για κάθε γειτονιά τον αριθμό που εμφανίζεται σε αυτη τη γειτονιά ο κάθε τύπος δωματίου 
### και στη συνέχεια βρίσκωτο τύπο δωματίου με τις μέγιστες εμφανίσεις
In [48]:
#1.8
Input[Input['price']==Input['price'].max()]
Max_price=Input[Input['price']==Input['price'].max()]
Max_type=Input['room_type'][Input['price']==Input['price'].max()].iloc[0]
e=Max_price['price'].iloc[1]
Text = str(e) + " - " + Max_type
Input['price'].plot()
plt.annotate(Text, xy=(1, e), xytext=(8, 0) ,
                xycoords=('axes fraction', 'data'), textcoords='offset points')
print('Max type room:',Max_type)
### Βρίσκω τη μεγιστη τιμή που εμφανίζεται σε δωμάτιο ,στη συνέχεια βρίσκω το τύπο-ους δωματίου που έχουν αυτή τη τιμή και τους εκτυπώνω 
Max type room: Private room
In [49]:
#1.10-Neighbourhood
stopwords = ["drink", "now", "wine", "flavor", "flavors"]
text=""
for val in Input['neighbourhood'].unique():
    text=text+" "+ val
wordcloud1= WordCloud(stopwords=stopwords,width=1000, height=1000).generate(text) 
plt.figure( figsize=(40,20))
plt.imshow(wordcloud1)
plt.axis("off")
##βάζω σε ένα text όλες τις περιοχές του Input και τις εμφανίζω σε ένα wordcloud
Out[49]:
(-0.5, 999.5, 999.5, -0.5)
In [50]:
#1.10-Transit
text1=""
for val in Input['transit'].unique():
    text1=text1+""+str(val)
wordcloud2= WordCloud(stopwords=stopwords,width=200, height=200,background_color="white", max_words=2).generate(text1) 
plt.figure()
plt.imshow(wordcloud2)
plt.axis("off")
plt.show()
#Βάζω στο text1 όλες τις τιμές του transit  και τις εμφανίζω σε ένα word_Cloud
In [51]:
#1.10-Description
text1=""
for val in Input['description']:
    text1=text1+""+str(val)
wordcloud3 = WordCloud(stopwords=stopwords,width=3000, height=3000,background_color="white").generate(text1) 
fig=plt.figure( figsize=(20,10), facecolor='k')
plt.imshow(wordcloud3)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
fig.savefig('wordcloud3.png', facecolor='k', bbox_inches='tight')
#Βάζω στο text1 όλες τις περιγραφές  του description   και τις εμφανίζω σε ένα word_Cloud
In [52]:
#1.3
e=Input.groupby(['neighbourhood']).count()
g=e.nlargest(5,'description')
t=g['description']
t.plot(kind='bar')
plt.ylabel('Description number')
plt.show()
#Βρίσκω για κάθε περιοχή πόσες κριτικές έχει και στη συνέχεια εμφανίζω τις 5 με τις πιο πολλές κριτικές
In [53]:
#1.9
ran=1
#February
e=Input[Input['Month']==ran]
map_osm = folium.Map(location=[40.742, -73.956], zoom_start=11)

e.apply(lambda row:folium.CircleMarker(location=[row["latitude"], row["longitude"]], 
                                              radius=10,popup=[row['Bed_type'],row['room_type'],row['transit']])
                                             .add_to(map_osm), axis=1)

map_osm
###Για το μήνα Φεβρουάριο εμφανίζω το χάρτη με τα ακίνητα  
Out[53]:
In [54]:
#1.12
#Ποια είναι η τιμή δωματίου για την οποία έχουμε τις περισσότερες καταχωρήσεις ακινήτων ?
e=Input.groupby(['price']).count()
g=e.nlargest(1,'id')
t=g['id']
print(t)
t.plot(kind='bar')
plt.ylabel('Entries Number')
plt.show()
price
40    1753
Name: id, dtype: int64
In [55]:
#1.13 
#Πόσες καταχωρήσεις υπάρχουν για την ακριβότερη-ες περιοχή της αθήνας το μήνα Απρίλιο
ran=3
#ran=3 για το μήνα απρίλιο
Max_price=Input[Input['price']==Input['price'].max()]
g=Input[Input['price']==Input['price'].max()]
e=g[g['Month']==ran]
d=e.groupby('neighbourhood')['id'].count()
r=Max_price['price'].iloc[1]
print('Max_price =',r)
d.plot(kind='bar',figsize=(20,10))
plt.ylabel('Number of entries')
plt.show()
Max_price = 7000
In [56]:
#1.10-Last_review
text=""
April_reviews=pd.read_csv(r'/home/prokopis/Desktop/data/april/reviews.csv',usecols=['listing_id','date','comments'])
March_reviews=pd.read_csv(r'/home/prokopis/Desktop/data/march/reviews.csv',usecols=['listing_id','date','comments'])
Fabrouary_reviews=pd.read_csv(r'/home/prokopis/Desktop/data/febrouary/reviews.csv',usecols=['listing_id','date','comments'])
April=Input[Input['Month']==3]
March=Input[Input['Month']==2]
February=Input[Input['Month']==1]
April_all=pd.merge(April, April_reviews,  how='left', left_on=['id','last_review'], right_on = ['listing_id','date'])
March_all=pd.merge(March, March_reviews,  how='left', left_on=['id','last_review'], right_on = ['listing_id','date'])
February_all=pd.merge(February, Fabrouary_reviews,  how='left', left_on=['id','last_review'], right_on = ['listing_id','date'])
ap=April_all['comments']
mar=March_all['comments']
feb=February_all['comments']
for i in ap:
    text=text+" "+str(i)
for i in mar:
    text=text+" "+str(i)
for i in feb:
    text=text+" "+str(i)
wordcloud1 = WordCloud(width=3000, height=3000,background_color="white").generate(text) 
plt.figure(figsize=(100,100))
plt.imshow(wordcloud1)
plt.axis("off")
plt.show()
#Για το reviews υπέθεσα ότι το κλειδί είναι το listing_id